package com.ld.hpfs.view.excel;

import java.io.File;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import com.ld.hpfs.common.utils.Const;
import com.ld.hpfs.common.utils.Tools;
import com.ld.hpfs.web.interseptor.WebConstants;

public class WriteBackExcelView extends AbstractExcelView {

	@Override
	protected void buildExcelDocument(Map<String, Object> model,
			HSSFWorkbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		// 取得模型数据
		List<HashMap<String, Object>> table = (List<HashMap<String, Object>>) model
				.get("table");
		// String title = "本地科目表";
		DecimalFormat fmt = new DecimalFormat("0.##");
		//
		// // 创建工作表和标题
		HSSFSheet sheet = workbook.createSheet(); // 创建工作区

		// 创建数据表头

		ArrayList<String> titles = new ArrayList<String>();
		// 动态取得表头
		HashMap<String, Object> titleNameMap = table.get(0);
		ArrayList<HashMap<String, Object>> titleName = (ArrayList<HashMap<String, Object>>) titleNameMap.get("columnnames");
		for (int i = 0; i < titleName.size(); i++) {
			titles.add(titleName.get(i).get("name").toString());
		}
		HSSFRow row = sheet.createRow(0);
		HSSFCellStyle items_style = workbook.createCellStyle();
		items_style.setAlignment((short) HSSFCellStyle.ALIGN_CENTER);

		HSSFFont celltbnamefont = workbook.createFont();
		celltbnamefont.setFontHeightInPoints((short) 10);

		celltbnamefont.setColor((short) (HSSFFont.COLOR_RED));
		items_style.setFont(celltbnamefont);
		items_style.setWrapText(true);

		for (int i = 0; i < titles.size(); i++) {
			HSSFCell cell = row.createCell(i);
			sheet.setColumnWidth(i, 5335);
			cell.setCellValue(titles.get(i));
			cell.setCellStyle(items_style);
		}

		HSSFCellStyle datestyle = workbook.createCellStyle();
		HSSFDataFormat df = workbook.createDataFormat();
		datestyle.setDataFormat(df.getFormat("yyyy-mm-dd"));

		int rowNum = 1;
		table.remove(0);

		try {
			for (HashMap<String, Object> entry : table) {
				HSSFRow datarow = sheet.createRow(rowNum++);
				for (int i = 0; i < titles.size(); i++) {	
					if(titles.get(i).toString().toLowerCase().equals("document_no")){//切换document_no的不规则数值
						datarow.createCell(i).setCellValue(
								entry.get("ttax_doc_no_temp")==null ? "": entry.get("ttax_doc_no_temp").toString());
					}else{
					    datarow.createCell(i).setCellValue(
							    entry.get(titles.get(i))==null ? "": entry.get(titles.get(i)).toString());
					}
				}
			}
			// 获取项目所在路径
			String strPath = Const.REAL_PATH;
			// 获取当前时间
			Date date = new Date();
			java.text.DateFormat format = new java.text.SimpleDateFormat(
					"yyyyMMddhhmmss");
			String time = format.format(date);
			if (strPath.contains("tomcat-7.0")) {
				String strfilePath = strPath.substring(0,
						strPath.indexOf("tomcat-7.0"))
						+ "download//";
				File file = new File(strfilePath + "EXCEL-" + time + ".xls");
				FileOutputStream os = new FileOutputStream(file);
				workbook.write(os);
				os.flush();
				os.close();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
